import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { debounceTime } from 'rxjs/operators/debounceTime';
import { map } from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import {
    PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
    CustomNavigation, KeyValueOutput, SearchValue
} from '../..';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';

@Component({
    selector: 'app-course-class',
    templateUrl: './course-class.component.html',
    styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class CourseClassComponent extends NgZorroPageBaseComponent implements OnInit {
    listUrl = 'ClassInfo/GetClassByName';
    searchChange$ = new BehaviorSubject(new SearchValue('', ''));
    optionList = [];
    selectedUser;
    /**
     * 是否从远程获取数据
     */
    isLoading = false;

    onSearch(value: string, url: string): void {
        this.isLoading = true;
        this.searchChange$.next(new SearchValue(value, this.listUrl));
    }

    constructor(
        injecter: Injector,
        public http: HttpClient) {
        super(injecter,
            new BaseComponentConfig(
                "Course/GetCourseClassInfo",
                "Course/AddCourseClass",
                "Course/Update",
                "Course/Get",
                "Course/DeleteCourseClass",
                "id",
                "ClassInfo",
                "班级、学生信息管理",
                null,
                "courseInfoId",
                { Edit: false, Dell: true },
            ));
        const optionList$: Observable<Array<KeyValueOutput>> =
            this.searchChange$.asObservable().pipe(debounceTime(1000)).pipe(
                switchMap((searchValue: SearchValue) => {
                    try {
                        if (searchValue.url) {
                            return this.httpService
                                .get<Array<KeyValueOutput>>(`${searchValue.url}?input=${searchValue.value}`).then(e => e);
                        } else {
                            return new Promise(resolve => {
                                resolve(new Array<KeyValueOutput>());
                            });
                        }
                    } catch {
                        return new Promise(resolve => {
                            resolve(new Array<KeyValueOutput>());
                        });
                    }

                }));
        optionList$.subscribe(data => {
            this.optionList = data;
            this.isLoading = false;
        });
        this.searchChange$.next(new SearchValue('', this.listUrl));
    }
    ngOnInit() {
        this.IniObserver.subscribe(sb => {
            this.getEntityList();
        });
    }
    getNewEntity(): ClassInfoOutput {
        return new ClassInfoOutput();
    }

    /**
    * 添加内容
    */
    async addEntity() {
        if (!this.selectedUser) {
            return;
        }
        this.isPending = true;
        try {
            await this.httpService.post<any>('Course/AddCourseClass', { id: this._pKeyFieldValue, ids: this.selectedUser });
        } finally {
            this.isPending = false;
        }
        this.getEntityList();
    }
}
